<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>数据统计 - 智慧农业信息管理系统</title>
    <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.3.0/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdn.bootcdn.net/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <style>
        body {
            background-color: #f5f7fa;
        }
        .navbar {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .chart-container {
            background: white;
            border-radius: 10px;
            padding: 20px;
            margin-bottom: 20px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.05);
        }
        .chart-title {
            font-size: 1.2rem;
            font-weight: 600;
            color: #333;
            margin-bottom: 15px;
            padding-bottom: 10px;
            border-bottom: 2px solid #667eea;
        }
    </style>
</head>
<body>
    <nav class="navbar navbar-expand-lg navbar-dark">
        <div class="container-fluid">
            <a class="navbar-brand" href="/index">🌾 智慧农业管理系统</a>
            <div class="navbar-nav ms-auto">
                <a class="btn btn-outline-light btn-sm" href="/index">返回首页</a>
            </div>
        </div>
    </nav>

    <div class="container-fluid mt-4">
        <h2 class="mb-4">📊 数据统计分析</h2>
        
        <div class="row">
            <!-- 生产成本统计 -->
            <div class="col-md-6">
                <div class="chart-container">
                    <div class="chart-title">
                        <i class="fas fa-coins me-2"></i>生产成本统计（按农事类型）
                    </div>
                    <div id="costChart" style="height: 350px;"></div>
                </div>
            </div>
            
            <!-- 销售额统计 -->
            <div class="col-md-6">
                <div class="chart-container">
                    <div class="chart-title">
                        <i class="fas fa-chart-line me-2"></i>销售额统计（按月份）
                    </div>
                    <div id="salesChart" style="height: 350px;"></div>
                </div>
            </div>
        </div>
        
        <div class="row">
            <!-- 产量统计 -->
            <div class="col-md-6">
                <div class="chart-container">
                    <div class="chart-title">
                        <i class="fas fa-weight-hanging me-2"></i>产量统计（按作物）
                    </div>
                    <div id="yieldChart" style="height: 350px;"></div>
                </div>
            </div>
            
            <!-- 销量统计 -->
            <div class="col-md-6">
                <div class="chart-container">
                    <div class="chart-title">
                        <i class="fas fa-shopping-cart me-2"></i>销量统计（按产品）
                    </div>
                    <div id="volumeChart" style="height: 350px;"></div>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.3.0/js/bootstrap.bundle.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/echarts/5.4.2/echarts.min.js"></script>
    
    <script>
        $(document).ready(function() {
            // 加载生产成本统计
            $.get('/statistics/cost', function(res) {
                if (res.code === 200) {
                    renderCostChart(res.data);
                }
            });
            
            // 加载销售额统计
            $.get('/statistics/sales', function(res) {
                if (res.code === 200) {
                    renderSalesChart(res.data);
                }
            });
            
            // 加载产量统计
            $.get('/statistics/yield', function(res) {
                if (res.code === 200) {
                    renderYieldChart(res.data);
                }
            });
            
            // 加载销量统计
            $.get('/statistics/volume', function(res) {
                if (res.code === 200) {
                    renderVolumeChart(res.data);
                }
            });
        });
        
        // 渲染生产成本图表
        function renderCostChart(data) {
            var chart = echarts.init(document.getElementById('costChart'));
            var typeMap = {
                'sowing': '播种',
                'fertilizing': '施肥',
                'irrigation': '灌溉',
                'weeding': '除草',
                'pesticide': '施药',
                'harvesting': '收获',
                'other': '其他'
            };
            
            var chartData = [];
            for (var key in data) {
                chartData.push({
                    name: typeMap[key] || key,
                    value: data[key]
                });
            }
            
            var option = {
                tooltip: {
                    trigger: 'item',
                    formatter: '{b}: ¥{c} ({d}%)'
                },
                legend: {
                    orient: 'vertical',
                    left: 'left'
                },
                series: [{
                    type: 'pie',
                    radius: '60%',
                    data: chartData,
                    emphasis: {
                        itemStyle: {
                            shadowBlur: 10,
                            shadowOffsetX: 0,
                            shadowColor: 'rgba(0, 0, 0, 0.5)'
                        }
                    }
                }]
            };
            chart.setOption(option);
        }
        
        // 渲染销售额图表
        function renderSalesChart(data) {
            var chart = echarts.init(document.getElementById('salesChart'));
            var months = [];
            var values = [];
            
            for (var key in data) {
                months.push(key);
                values.push(data[key]);
            }
            
            var option = {
                tooltip: {
                    trigger: 'axis',
                    formatter: '{b}<br/>销售额: ¥{c}'
                },
                xAxis: {
                    type: 'category',
                    data: months
                },
                yAxis: {
                    type: 'value',
                    name: '金额(元)'
                },
                series: [{
                    data: values,
                    type: 'line',
                    smooth: true,
                    areaStyle: {
                        color: 'rgba(102, 126, 234, 0.2)'
                    },
                    itemStyle: {
                        color: '#667eea'
                    }
                }]
            };
            chart.setOption(option);
        }
        
        // 渲染产量图表
        function renderYieldChart(data) {
            var chart = echarts.init(document.getElementById('yieldChart'));
            var crops = [];
            var values = [];
            
            for (var key in data) {
                crops.push(key);
                values.push(data[key]);
            }
            
            var option = {
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {
                        type: 'shadow'
                    },
                    formatter: '{b}<br/>产量: {c}斤'
                },
                xAxis: {
                    type: 'category',
                    data: crops
                },
                yAxis: {
                    type: 'value',
                    name: '产量(斤)'
                },
                series: [{
                    data: values,
                    type: 'bar',
                    itemStyle: {
                        color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                            { offset: 0, color: '#83bff6' },
                            { offset: 1, color: '#188df0' }
                        ])
                    }
                }]
            };
            chart.setOption(option);
        }
        
        // 渲染销量图表
        function renderVolumeChart(data) {
            var chart = echarts.init(document.getElementById('volumeChart'));
            var products = [];
            var values = [];
            
            for (var key in data) {
                products.push(key);
                values.push(data[key]);
            }
            
            var option = {
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {
                        type: 'shadow'
                    },
                    formatter: '{b}<br/>销量: {c}斤'
                },
                xAxis: {
                    type: 'category',
                    data: products,
                    axisLabel: {
                        interval: 0,
                        rotate: 30
                    }
                },
                yAxis: {
                    type: 'value',
                    name: '销量(斤)'
                },
                series: [{
                    data: values,
                    type: 'bar',
                    itemStyle: {
                        color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                            { offset: 0, color: '#f093fb' },
                            { offset: 1, color: '#f5576c' }
                        ])
                    }
                }]
            };
            chart.setOption(option);
        }
    </script>
</body>
</html>

